﻿using Grpc.Core;
using Grpc.Net.Client;
using System;
using System.Threading;
using System.Threading.Tasks;
using Zhaoxi.GrpcService;

namespace Zhaoxi.GrpcClient
{
	class Program
	{
		//Grpc.Net.Client，其中包含.NET Core 客户端。
		//Google.Protobuf 包含适用于 C# 的 Protobuf 消息。
		//Grpc.Tools 包含适用于 Protobuf 文件的 C# 工具支持。 运行时不需要工具包，因此依赖项标记为 
		static void Main(string[] args)
		{
			UnitTest.Show();
			//MyConsul.ShowAsync();

			//Console.WriteLine("Hello World!");
			//{
			//	Console.WriteLine("**************");
			//	TestHello().Wait();
			//}
		}

		private static async Task TestHello()
		{
			using (var channel = GrpcChannel.ForAddress("https://localhost:5001"))
			{
				//var client = new Greeter.GreeterClient(channel);
				//var reply = await client.SayHelloAsync(new HelloRequest { Name = "Clay老师" });
				//Console.WriteLine("服务端说: " + reply.Message);

				////client.SayHello(new HelloRequest { Name = "Clay" });
				var client = new CustomMath.CustomMathClient(channel);
				#region 自定义

				//Console.WriteLine("************简单调用************");
				//{

				//	var reply = await client.SayHelloAsync(new HelloRequestMath() { Name = "clay" });
				//	Console.WriteLine("服务端说: " + reply.Message);

				//}

				#region 客户端流
				//Console.WriteLine("**************************客户端流*****************************");
				//{
				//	var bathCat = client.SelfIncreaseClient();
				//	for (int i = 0; i < 10; i++)
				//	{
				//		await bathCat.RequestStream.WriteAsync(new BathTheCatReq() { Id = new Random().Next(0, 20) });
				//		await Task.Delay(100);
				//		Console.WriteLine($"This is {i} Request {Thread.CurrentThread.ManagedThreadId}");
				//	}
				//	Console.WriteLine("**********************************");
				//	//发送完毕
				//	await bathCat.RequestStream.CompleteAsync();
				//	Console.WriteLine("客户端已发送完10个id");
				//	Console.WriteLine("接收结果：");

				//	foreach (var item in bathCat.ResponseAsync.Result.Number)
				//	{
				//		Console.WriteLine($"This is {item} Result");
				//	}
				//	Console.WriteLine("**********************************");
				//}
				#endregion

				#region 服务端流
				//Console.WriteLine("**************************服务端流*****************************");
				//{
				//	IntArrayModel intArrayModel = new IntArrayModel();
				//	for (int i = 0; i < 15; i++)
				//	{
				//		intArrayModel.Number.Add(i);//Number不能直接赋值，
				//	}

				//	CancellationTokenSource cts = new CancellationTokenSource();
				//	cts.CancelAfter(TimeSpan.FromSeconds(2.5)); //指定在2.5s后进行取消操作
				//	var bathCat = client.SelfIncreaseServer(intArrayModel, cancellationToken: cts.Token);

				//	//var bathCat = client.SelfIncreaseServer(intArrayModel);//不带取消
				//	var bathCatRespTask = Task.Run(async () =>
				//	{
				//		await foreach (var resp in bathCat.ResponseStream.ReadAllAsync())
				//		{
				//			Console.WriteLine(resp.Message);
				//			Console.WriteLine($"This is  Response {Thread.CurrentThread.ManagedThreadId}");
				//			Console.WriteLine("**********************************");
				//		}
				//	});
				//	Console.WriteLine("客户端已发送完10个id");
				//	//开始接收响应
				//	await bathCatRespTask;
				//} 
				#endregion


				#region 双流
				Console.WriteLine("**************************这是双流呀*****************************");
				{
					var bathCat = client.SelfIncreaseDouble();
					var bathCatRespTask = Task.Run(async () =>
					{
						await foreach (var resp in bathCat.ResponseStream.ReadAllAsync())
						{
							Console.WriteLine(resp.Message);
							Console.WriteLine($"This is  Response {Thread.CurrentThread.ManagedThreadId}");
							Console.WriteLine("**********************************");
						}
					});
					for (int i = 0; i < 10; i++)
					{
						await bathCat.RequestStream.WriteAsync(new BathTheCatReq() { Id = new Random().Next(0, 20) });
						await Task.Delay(100);
						Console.WriteLine($"This is {i} Request {Thread.CurrentThread.ManagedThreadId}");
						Console.WriteLine("**********************************");
					}
					//发送完毕
					await bathCat.RequestStream.CompleteAsync();
					Console.WriteLine("客户端已发送完10个id");
					Console.WriteLine("接收结果：");
					//开始接收响应
					await bathCatRespTask;
				}
				#endregion

				#endregion


				#region 取消
				//Console.WriteLine("**************************双流+取消*****************************");
				//{
				//	CancellationTokenSource cts = new CancellationTokenSource();
				//	cts.CancelAfter(TimeSpan.FromSeconds(2.5)); //指定在2.5s后进行取消操作
				//	var bathCat = client.SelfIncreaseDouble(cancellationToken: cts.Token);
				//	var bathCatRespTask = Task.Run(async () =>
				//	{
				//		await foreach (var resp in bathCat.ResponseStream.ReadAllAsync())
				//		{
				//			Console.WriteLine(resp.Message);
				//			Console.WriteLine($"This is  Response {Thread.CurrentThread.ManagedThreadId}");
				//			Console.WriteLine("**********************************");
				//		}
				//	});
				//	for (int i = 0; i < 10; i++)
				//	{
				//		await bathCat.RequestStream.WriteAsync(new BathTheCatReq() { Id = new Random().Next(0, 20) });
				//		await Task.Delay(100);
				//		Console.WriteLine($"This is {i} Request {Thread.CurrentThread.ManagedThreadId}");
				//		Console.WriteLine("**********************************");
				//	}
				//	//发送完毕
				//	await bathCat.RequestStream.CompleteAsync();
				//	Console.WriteLine("客户端已发送完10个id");
				//	Console.WriteLine("接收结果：");
				//	//开始接收响应
				//	await bathCatRespTask;
				//} 
				#endregion
			}
		}
	}
}
